package com.ibm.mall.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ibm.mall.base.BaseAction;
import com.ibm.mall.model.Feedback;
import com.ibm.mall.model.PageBean;
import com.ibm.mall.model.ShippingAddress;
import com.ibm.mall.model.User;
import com.ibm.mall.util.MD5Util;
import com.ibm.mall.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class CustomerUserAction extends BaseAction<User> {

	private static final long serialVersionUID = 1L;
	
	private String newPassword;
	
	private Integer pageNum;
	
	private ShippingAddress contact;
	
	private Integer contactId;
	
	private String feedbackContent;
	
	public String myAccount() {
		User user=(User) ActionContext.getContext().getSession().get("user");
		User u=userService.getUserByUserAccount(user.getUserAccount());
		ActionContext.getContext().put("u", u);
		return "showAccount";
	}
	
	public String myDataSet() {
		User user=(User) ActionContext.getContext().getSession().get("user");
		User u=userService.getUserByUserAccount(user.getUserAccount());
		ActionContext.getContext().put("u", u);
		ActionContext.getContext().put("flag", 0);
		return "showDataSet";
	}
	
	public String updateDataSet() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user=(User) ActionContext.getContext().getSession().get("user");
		user.setUserName(model.getUserName());
		user.setUserGender(model.getUserGender());
		user.setUserEmail(model.getUserEmail());
		user.setUserPhone(model.getUserPhone());
		user.setUserIdcard(model.getUserIdcard());
		user.setLastUpdateTime(sf.format(new Date()));
		userService.updateUser(user);
		return "toDataSet";
	}
	
	public String updatePassword() {
		User user=(User) ActionContext.getContext().getSession().get("user");
		if(user.getUserPassword().equals(MD5Util.MD5(model.getUserPassword()))){
			user.setUserPassword(MD5Util.MD5(newPassword));
			userService.updateUser(user);
			return "toLogout";
		}else{
			ActionContext.getContext().put("msg", "原密码不正确！");
			ActionContext.getContext().put("u", user);
			ActionContext.getContext().put("flag", 1);
			return "showDataSet";
		}
	}
	
	public String myContact() {
		User user=(User) ActionContext.getContext().getSession().get("user");
		if(pageNum==null){
			pageNum=1;
		}
		int pageSize=10;
		QueryHelper queryHelper=new QueryHelper(ShippingAddress.class,"s")
			.addCondition("s.user=?", user);
		PageBean contactPageBean=shippingAddressService.getPageBean(pageNum,pageSize,queryHelper);
		ActionContext.getContext().put("contactPageBean", contactPageBean);
		return "showContact";
	}
	
	public String addContact() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user=(User) ActionContext.getContext().getSession().get("user");
		contact.setInsertTime(sf.format(new Date()));
		contact.setStatus((short) 0);
		contact.setUser(user);
		shippingAddressService.addContact(contact);
		return "toShowContact";
	}
	
	public String editContact() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user=(User) ActionContext.getContext().getSession().get("user");
		contact.setLastUpdateTime(sf.format(new Date()));
		contact.setUser(user);
		shippingAddressService.updateContact(contact);
		return "toShowContact";
	}
	
	public String deleteContact() {
		shippingAddressService.deleteContact(contactId);
		return "toShowContact";
	}
	
	public String myComplain() {
		if(pageNum==null){
			pageNum=0;
		}
		int pageSize=10;
		User user=(User) ActionContext.getContext().getSession().get("user");
		QueryHelper queryHelper=new QueryHelper(Feedback.class, "f")
			.addCondition("f.status=?", (short)0)
			.addCondition("f.user=?", user);
		PageBean complainPageBean=feedbackService.getPageBean(pageNum, pageSize, queryHelper);
		ActionContext.getContext().put("complainPageBean", complainPageBean);
		return "showComplain";
	}
	
	public String addComplain() {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user=(User) ActionContext.getContext().getSession().get("user");
		Feedback feedback=new Feedback();
		feedback.setFeedbackContent(feedbackContent);
		feedback.setFeedbackStatus((short) 0);
		feedback.setInsertTime(sf.format(new Date()));
		feedback.setStatus((short) 0);
		feedback.setUser(user);
		feedbackService.saveFeedback(feedback);
		return "toShowComplain";
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public ShippingAddress getContact() {
		return contact;
	}

	public void setContact(ShippingAddress contact) {
		this.contact = contact;
	}

	public Integer getContactId() {
		return contactId;
	}

	public void setContactId(Integer contactId) {
		this.contactId = contactId;
	}

	public String getFeedbackContent() {
		return feedbackContent;
	}

	public void setFeedbackContent(String feedbackContent) {
		this.feedbackContent = feedbackContent;
	}

}
